package com.ruoyi.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.ruoyi.system.domain.CompeWorkScroingRecord;
import com.ruoyi.system.domain.SysCertificate;
import com.ruoyi.system.domain.SysUsers;
import com.ruoyi.system.domain.Work;
import com.ruoyi.system.domain.mongodb.Users;
import com.ruoyi.system.domain.mongodb.Works;
import com.ruoyi.system.mapper.CompeWorkScroingRecordMapper;
import com.ruoyi.system.mapper.WorkMapper;
import com.ruoyi.system.mongo.SysUserDao;
import com.ruoyi.system.mongo.WorksDao;
import com.ruoyi.system.service.ISysUsersService;
import com.ruoyi.system.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author 柳
 * @Date 2021/3/5
 * @Description TODO
 */
@Service
public class WorkServiceImpl implements WorkService {
    @Autowired
    private WorkMapper workMapper;
    @Autowired
    private CompeWorkScroingRecordMapper compeWorkScroingRecordMapper;
    @Autowired
    private ISysUsersService sysUsersService;

    @Autowired
    private WorksDao worksDao;
    @Autowired
    private SysUserDao sysUserDao;
    /**
     * 根据作品id查询作品信息
     * @param sysCertificate
     * @return
     */
    @Override
    public Work queryWorkByWorkid(SysCertificate sysCertificate) {
        return workMapper.queryWorkByWorkid(sysCertificate);
    }

    /**
     * 根据赛事id查询是否有大众评审作品
     * @param epid
     * @return
     */
    @Override
    public List<Work> queryReviewWorkByEpid(Integer epid) {
        return workMapper.queryReviewWorkByEpid(epid);
    }

    /**
     * 根据大赛id查询作品信息(填充下拉框)
     * @param epid
     * @return
     */
    @Override
    public List<Work> selectWorkByEpid(Integer epid) {
        return workMapper.selectWorkByEpid(epid);
    }

    /**
     * 增加大赛作品投票数
     * @param work
     */
    @Override
    public void addVoteCount(Work work) {
        workMapper.addVoteCount(work);
    }

    /**
     * 改变作品状态为大众评审作品
     * @param work_id
     */
    @Override
    public void updateReviewStateByWorkId(String work_id) {
        workMapper.updateReviewStateByWorkId(work_id);
    }

    /**
     * 教师端根据教师id查询待评审作品信息
     * @param work
     * @return
     */
    @Override
    public List<Work> queryWorksByTid(Work work) {
        Users users = sysUserDao.queryUserById(work.getTid());
        //创建存放未评审作品的集合
        List<Work> workList= null;
        //如果是校园管理员
        if (users != null&& users.getIsAdmin() == 1) {
            workList = workMapper.queryWorksByTidForAdmin(work);
        } else {
            workList = workMapper.queryWorksByTid(work);
        }
        return workList;
    }

    /**
     * 根据作品id修改投票数
     * @param work_id
     */
    @Override
    public void updateVote(String work_id) {
        workMapper.updateVote(work_id);
    }

    /**
     * 根据大赛id查询作品信息
     * @param epid
     * @return
     */
    @Override
    public List<Work> queryWorkByEpid(Integer epid) {
        return workMapper.queryWorkByEpid(epid);
    }

    /**
     * 根据大赛id查询参加大众评审的作品信息
     * @param epid
     * @return
     */
    @Override
    public List<Map<String, Object>> queryPublicReviewWorkByEpid(Integer epid, String event_step){
        return workMapper.queryPublicReviewWorkByEpid(epid, event_step);
    }
    /**
     * 查询当前品评审员负责的大赛作品信息
     * @param work
     * @return
     */
    @Override
    public List<Work> queryWorks(Work work) {
//
//        List<Work> workList=new ArrayList<>();
//        for (Work queryWork : workMapper.queryWorks(work)) {
//            CompeWorkScroingRecord compeWorkScroingRecord=new CompeWorkScroingRecord();
//            compeWorkScroingRecord.setCompe_id(queryWork.getEpid());
//            compeWorkScroingRecord.setWork_id(queryWork.getWork_id());
//            compeWorkScroingRecord.setUser_id(queryWork.getUser_id());
//            CompeWorkScroingRecord record = compeWorkScroingRecordMapper.queryCompeWorkScroingRecordIsNull(compeWorkScroingRecord);
//            if(record==null){
//                workList.add(queryWork);
//            }
//        }
        return workMapper.queryWorks(work);
    }

    @Override
    public List<Work> queryWorksForAdmin(Work work) {
//        List<Work> workList=new ArrayList<>();
//        for (Work queryWork : ) {
//            CompeWorkScroingRecord compeWorkScroingRecord=new CompeWorkScroingRecord();
//            compeWorkScroingRecord.setCompe_id(queryWork.getEpid());
//            compeWorkScroingRecord.setWork_id(queryWork.getWork_id());
//            compeWorkScroingRecord.setUser_id(queryWork.getUser_id());
//            CompeWorkScroingRecord record = compeWorkScroingRecordMapper.queryCompeWorkScroingRecordIsNull(compeWorkScroingRecord);
//            if(record==null){
//                workList.add(queryWork);
//            }
//        }
        return workMapper.queryWorksForAdmin(work);
    }


    /**
     * 根据用户id,查询负责赛事下的所有作品id
     * @param userId
     * @return
     */
    @Override
    public List<String> queryWorkIdByUserId(long userId) {
        return workMapper.queryWorkIdByUserId(userId);
    }

    @Override
    public List<Work> selectWorkBySid(String sid) {
        List<Work> works = workMapper.selectWorkBySid(sid);
        return works;
    }


}
